package com.qf.co.Dao;

import com.qf.co.pojo.User;
import com.qf.co.utils.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author hanjiejie
 * @version V1.0
 * @Project java2203
 * @Package com.qf.co.Dao
 * @Description:
 * @Date 2022/5/27 13:59
 */
public class UserDaoImpl implements  UserDao {
    //创建Dbutil工具
    private QueryRunner queryRunner =  new QueryRunner(DBUtil.getDateSource());
    @Override
    public int AddUser(User user) {
        String sql =" insert into t_user values(null,?,?,?)";
        try {
            int count = queryRunner.update(sql, user.getUsername(), user.getPassword(),user.getStatus());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public User findByUserName(String username) {
        String sql ="select *from t_user where username=?";
        try {
            User user = queryRunner.query(sql, new BeanHandler<User>(User.class), username);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


}
